package ru.arriah.servicedesk.web.chief.action;

import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Locale;

import javax.naming.NamingException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ru.arriah.common.web.action.ActionMapping;
import ru.arriah.common.web.action.DumbAction;
import ru.arriah.servicedesk.bean.MainReportBean;
import ru.arriah.servicedesk.ejb.RequestManagerLocalHome;
import ru.arriah.servicedesk.ejb.RequestManagerLocalObject;
import ru.arriah.servicedesk.help.Utils;
import ru.arriah.servicedesk.web.commonaction.exception.LoginException;

public class MainReportAction extends DumbAction {
   protected RequestManagerLocalHome requestManagerHome;
   
   public MainReportAction() throws NamingException {
      super();
      requestManagerHome = 
         Utils.getRequestManagerHomeInterface();
   }
   
   public String execute(
      ActionMapping actionMapping,
      HttpServletRequest request,
      HttpServletResponse response
   ) throws IOException, ServletException {
      try {
         RequestManagerLocalObject requestManager =
            requestManagerHome.create();
         
         Action action = Action.valueOf(actionMapping.getActionName());
         
         Calendar calendar = Calendar.getInstance();         
         Date endDate = null;
         Date startDate = null;         
         
         switch (action) {
         case displayMainReport:                       
            endDate = calendar.getTime();
            calendar.add(Calendar.MONTH, -1);
            startDate = calendar.getTime();                    
         break;
         
         case processMainReport:
            DateFormat dateformat = DateFormat.getDateInstance(
               DateFormat.SHORT, 
               new Locale("ru")
            );            
                        
            startDate = calendar.getTime();
            
            try {
               endDate = dateformat.parse(request.getParameter("endDate"));
            } catch (Exception e) {
               endDate = calendar.getTime();
            }
            
            calendar.add(Calendar.MONTH, -1);
            
            try {
               startDate = dateformat.parse(request.getParameter("startDate"));
            } catch (Exception e) {
               startDate = calendar.getTime();
            }
                        
         break;
         default:
            throw new RuntimeException("Unsupported action " + action);
         }
         
         Collection<MainReportBean> mainReport = 
            requestManager.getMainReport(startDate, endDate);
                  
         request.setAttribute("reportList", mainReport);
         request.setAttribute("startDate", startDate);                                                
         request.setAttribute("endDate", endDate);
         
         requestManager.remove();         
         return actionMapping.getSuccessTarget();
      } catch (Exception e) {
         handleException(e, "execute");
         throw new LoginException("ErrorMessage.illegalLogin");
      }
   }

}